package com.liy.redisson.service;

import com.liy.redisson.entity.HelloEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.util.concurrent.Future;

@Service
public class HelloThreadServer {

    private final Logger logger = LoggerFactory.getLogger(HelloThreadServer.class);

    /**
     * @Async标注这个方法是一个异步方法
     * 将会在独立的线程中被执行，调用者无需等待他完成，即可继续执行其他操作
     */
    @Async
    public String sayHello(){
        for (int i = 0; i < 5; i++) {
            logger.info(""+i);
        }
        logger.info("start say hello");
        System.out.println(Thread.currentThread().getName());
        System.out.println("Hello");
        logger.info("end say Hello");
        return "你好";
    }
    @Async
    public void sayHelloW(){
        logger.info("ssss");
        System.out.println(Thread.currentThread().getName());
        System.out.println("ss");
        logger.info("essssss");
    }

    @Async
    public Future<HelloEntity> getHelloToString(){
        logger.info("start getHelloString");
        HelloEntity helloEntity = new HelloEntity();
        helloEntity.setHelloStr("Say hello to little wang");
        System.out.println(Thread.currentThread().getName());
        logger.info("end getHelloString");
        return new AsyncResult<>(helloEntity);
    }

}
